<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">

    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>用户管理</h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>统计分析</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="用户/手机号" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findPage()" class="dalfBut" icon="el-icon-search" type="success" plain
                           style="color:limegreen">查询
                </el-button>
                <el-button class="butT" @click="handleCreate()" icon="el-icon-circle-plus-outline" type="primary" plain
                           style="color:dodgerblue">新建
                </el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号" width="120"></el-table-column>
                <el-table-column prop="id" label="用户ID" align="center" width="120"></el-table-column>
                <el-table-column prop="username" label="用户账号" align="center" width="120"></el-table-column>
                <el-table-column prop="gender" label="用户性别" align="center" width="120"></el-table-column>
                <el-table-column prop="station" label="用户状态" align="center" width="120"></el-table-column>
                <el-table-column prop="birthday" label="生日" align="center" width="120"></el-table-column>
                <el-table-column prop="telephone" label="手机号码" align="center" width="120"></el-table-column>
                <el-table-column prop="remake" label="用户备注" align="center" width="120"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="handleUpdate(scope.row)">编辑
                        </el-button>
                        <el-button size="mini" icon="el-icon-delete" type="danger" @click="handleDelete(scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增管理" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px" :model="formData" status-icon
                                         :rules="rules" ref="formData">
                                    <el-row>
                                        <el-col :span="16">
                                            <el-form-item label="用户账号" prop="username">
                                                <el-input v-model="formData.username"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="16">
                                            <el-form-item label="密码" prop="password">
                                                <el-input type="password" v-model="formData.password"
                                                          autocomplete="off"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="16">
                                            <el-form-item label="确认密码" prop="checkPass">
                                                <el-input type="password" v-model="formData.checkPass"
                                                          autocomplete="off"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="16" label="适用性别">
                                            <!--下拉框-->
                                            <el-form-item label="适用性别">
                                                <el-select v-model="formData.gender" placeholder="请选择">
                                                    <el-option label="男" value="男"></el-option>
                                                    <el-option label="女" value="女"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="16">
                                            <el-form-item label="状态">
                                                <el-input v-model="formData.station"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="16">
                                            <el-form-item label="手机号码">
                                                <el-input v-model="formData.telephone"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="备注">
                                                <el-input v-model="formData.remake" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>

                            <el-tab-pane label="管理角色" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>角色名称</th>
                                            <th>角色关键字</th>
                                            <th>角色描述</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.keyword}}</label></td>
                                            <td><label :for="c.id">{{c.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="submitForm('formData')">提交</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑套餐 -->

            <div class="edit-form">
                <el-dialog title="编辑用户" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px" :model="formData" status-icon
                                         ref="formData2">
                                    <el-row>
                                        <el-col :span="16">
                                            <el-form-item label="用户账号" prop="username">
                                                <el-input v-model="formData.username"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="16">
                                            <el-form-item label="密码" >
                                                <el-input type="password" v-model="formData.password"
                                                          autocomplete="off"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="16" label="适用性别">
                                            <!--下拉框-->
                                            <el-form-item label="适用性别">
                                                <el-select v-model="formData.gender" placeholder="请选择">
                                                    <el-option label="男" value="男"></el-option>
                                                    <el-option label="女" value="女"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="16">
                                            <el-form-item label="状态">
                                                <el-input v-model="formData.station"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="16">
                                            <el-form-item label="手机号码">
                                                <el-input v-model="formData.telephone"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="备注">
                                                <el-input v-model="formData.remake" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>

                            <el-tab-pane label="管理角色" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>角色名称</th>
                                            <th>角色关键字</th>
                                            <th>角色描述</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.keyword}}</label></td>
                                            <td><label :for="c.id">{{c.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="submitForm2('formData2')">提交</el-button>
                    </div>
                </el-dialog>
            </div>

        </div>
    </div>
</div>
</body>
<script>
    var vue = new Vue({
        el: '#app',
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === undefined || value === "") {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.formData.checkPass !== undefined) {
                        this.$refs.formData.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === undefined || value === "") {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.formData.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            /*   var validatePass3 = (rule, value, callback) => {
                   if (value ===undefined) {
                       callback(new Error('用户名不能为空'));
                   }else {
                       callback();
                   }
               };
               var validatePass4 = (rule, value, callback) => {
                   if (this.formData.checkPass !== "") {
                       this.$refs.formData2.validateField('checkPass');
                   }else if(value === ""){
                       callback();
                   }

               };
               var validatePass5 = (rule, value, callback) => {
                   if (value !== this.formData.password) {
                       callback(new Error('两次输入密码不一致!'));
                   } else if (value==="") {
                       callback()
                   }
               };*/
            return {
                activeName: 'first',//添加/编辑窗口Tab标签名称
                pagination: {//分页相关属性
                    currentPage: 1,
                    pageSize: 10,
                    total: 100,
                    queryString: null,
                },
                dataList: [],
                //列表数据
                formData: {},//基本信息数据
                tableData: [],//管理角色数据
                roleIds: [],//添加表单窗口中检查组复选框对应id
                dialogFormVisible: false,//控制添加窗口显示/隐藏
                dialogFormVisible4Edit: false,//控制编辑窗口显示/隐藏

                rules: {
                    username: [
                        {required: true, message: '项目编码为必填项', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, validator: validatePass, trigger: 'blur'}
                    ],
                    checkPass: [
                        {validator: validatePass2, trigger: 'blur'}
                    ],
                },
                /*  rules2: {
                      username:[
                          {validator: validatePass3, trigger: 'blur' }
                      ],
                      password: [
                          {validator: validatePass4, trigger: 'blur'}
                      ],
                      checkPass: [
                          {validator: validatePass5, trigger: 'blur'}
                      ],
                  }*/
            };
        },
        created() {
            this.findAll();
        },
        methods: {
            restForm() {
                this.formData = {};
                this.tableData = [];
                this.$refs["formData"].resetFields();
            },

            handleCreate() {
                this.dialogFormVisible = true;
                this.restForm();
                axios.get("/userManager/findRoles.do").then(res => {
                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? 'success' : 'error'
                    });
                    this.tableData = res.data.data;
                    axios.get("/userManager/findRoles.do?userId=" + row.id).then(res => {
                        this.tableData = res.data.data;
                    });
                });
            },

            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post("/userManager/add.do?roleIds=" + this.roleIds, this.formData).then(res => {
                            //关闭窗口
                            this.dialogFormVisible = false;
                            //res.data == Result
                            if (res.data.flag) {
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                                this.findAll();
                            }
                            else {
                                this.$message({
                                    message: res.data.message,
                                    type: 'error'
                                });
                            }
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },

            submitForm2(formName) {


                this.$refs[formName].validate((valid) => {
                    if (valid) {

                        axios.post("/userManager/edit.do?roleIds=" + this.roleIds, this.formData).then(res => {
                            //关闭窗口
                            this.dialogFormVisible4Edit = false;
                            //res.data == Result
                            if (res.data.flag) {
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                                this.findAll();
                                this.restForm();
                            }
                            else {
                                this.$message({
                                    message: res.data.message,
                                    type: 'error'
                                });
                            }
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },

            handleUpdate(row) {
                this.dialogFormVisible4Edit = true;

                //发送编辑弹窗回显数据请求
                axios.get("/userManager/findById.do?userId=" + row.id).then(res => {
                    if (res.data.flag) {
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                        this.formData = res.data.data;
                        this.formData.password = "";
                        this.formData.checkPass = "";

                        axios.get("/userManager/findRoles.do?userId=" + row.id).then(res => {
                            this.tableData = res.data.data;
                        });

                        axios.get("/userManager/findRoleIdsByUserId.do?userId=" + row.id).then(res => {
                            this.roleIds = res.data;
                        });
                    }
                })
            },

            findAll() {
                var queryPageBean = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };

                axios.post("/userManager/findAll.do", queryPageBean).then(res => {
                    this.pagination.total = res.data.data.total;
                    this.dataList = res.data.data.rows;
                }).catch((error) => {
                    this.showMessage(error);
                });
            },

            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findAll();
            },

            handleDelete(row) {
                axios.get("/userManager/deleteUser.do?userId=" + row.id).then(res => {
                    if (res.data.flag) {
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                        this.findAll();
                    }
                    else {
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                });
            },
            findPage() {
                var queryPageBean = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                axios.post("/userManager/findByQueryString.do?", queryPageBean).then(res => {
                    if (res.data.flag) {
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                        this.dataList = res.data.data.rows;
                    }
                    else {
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                });
            }
        },
    });
</script>
</html>